
/** 网络请求 */


var webUrl = "http://172.17.11.12:7001/";

/** 
 * GET 网络请求
 * url:请求地址
 * params：请求参数
 * onSuccess: status = SUCCESS
 * onFail: status 其他情况
 * aFail:请求失败
 */
function getRequest(url, params, requestHeader, onSuccess, onFail, aFail){

  wx.showLoading({
    title: '正在加载',
  })

  wx.request({
    url: webUrl + url,
    data: params,
    header: requestHeader,
    method: 'GET',
    success: function (res) {
      wx.hideLoading();

      if (res.statusCode == 200) {
        
        if (res.data.status == 'SUCCESS')
        {
          onSuccess(res.data);
        }
        else
        {
          onFail(res.data);
        }
      }
      else{
        console.log(res);
        aFail(res.data);
      }
    },
    fail: function (res) {
      wx.hideLoading();
      aFail(res.data);
    }
  })
}

/** 
 * POST 网络请求
 * url:请求地址
 * params：请求参数
 * onSuccess: status = SUCCESS
 * onFail: status 其他情况
 * aFail:请求失败
 */
function postRequest(url, params, requestHeader, onSuccess, onFail, aFail) {

  wx.showLoading({
    title: '正在加载',
  })

  wx.request({
    url: webUrl + url,
    data: params,
    header: requestHeader,
    method: 'POST',
    success: function (res) {
      wx.hideLoading();

      if (res.statusCode == 200) {

        console.log('', res);
        if (res.data.status == 'SUCCESS') {
          onSuccess(res.data);
        }
        else {
          onFail(res.data);
        }
      }
      else {
        console.log(res);
        aFail(res.data);
      }
    },
    fail: function (res) {
      wx.hideLoading();
      aFail(res.data);
    }
  })
}


/** 
 * putRequest 网络请求
 * url:请求地址
 * params：请求参数
 * onSuccess: status = SUCCESS
 * onFail: status 其他情况
 * aFail:请求失败
 */
function putRequest(url, params, requestHeader, onSuccess, onFail, aFail) {

  wx.showLoading({
    title: '正在加载',
  })

  wx.request({
    url: webUrl + url,
    data: params,
    header: requestHeader,
    method: 'PUT',
    success: function (res) {
      wx.hideLoading();

      if (res.statusCode == 200) {

        console.log('', res);
        if (res.data.status == 'SUCCESS') {
          onSuccess(res.data);
        }
        else {
          onFail(res.data);
        }
      }
      else {
        console.log(res);
        aFail(res.data);
      }
    },
    fail: function (res) {
      wx.hideLoading();
      aFail(res.data);
    }
  })
}


//导出模块
module.exports = {
  getRequest: getRequest,
  postRequest: postRequest,
  putRequest: putRequest
}